package com.jdk.demo.thread.completableFuture;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

// thenAccept 消费处理结果
//接收任务的处理结果，并消费处理，无返回结果
public class ThenAcceptDemo {

	public static void main(String[] args) throws InterruptedException {
		ExecutorService executorService = Executors.newFixedThreadPool(2);
        CompletableFuture cf = CompletableFuture.supplyAsync(() -> { //实现了Supplier的get()方法
            try {
                Thread.sleep(2000);
            } catch (Exception e) {
                e.printStackTrace();
            }
            System.out.println("supplyAsync " + Thread.currentThread().getName());
            return "hello ";
        }, executorService)
        // thenAccept()可以理解为接收CompletableFuture的结果然后再进行处理
        .thenAccept(s -> {
            try {
                System.out.println(s + "world " + Thread.currentThread().getName());
            } catch (Exception e) {
                e.printStackTrace();
            }
        })
        .thenAccept(s -> {
            try {
                System.out.println(s + "world2 " + Thread.currentThread().getName());
            } catch (Exception e) {
                e.printStackTrace();
            }
        });

        System.out.println(Thread.currentThread().getName());
        while (true) {
            if (cf.isDone()) {
                System.out.println("CompletedFuture...isDown " + Thread.currentThread().getName());
                break;
            }
            Thread.sleep(5000);
        }
	}

}
